<script setup lang="ts">
import { reactive, ref } from 'vue'

import { onLoad } from '@dcloudio/uni-app'
import { getRecommend, GoodsItem } from '@/apis/recommend'

const typeMap = [
  { type: '1', url: '/hot/preference', title: '特惠推荐' },
  { type: '2', url: '/hot/inVogue', title: '爆款推荐' },
  { type: '3', url: '/hot/oneStop', title: '一站买全' },
  { type: '4', url: '/hot/new', title: '领券中心' },
  { type: '5', url: '/hot/new', title: '新鲜好物' },
]
const bannerPicture = ref('')
type SubTypesAndGoods = GoodsItem & { id: string; title: string, }
const subTypes = ref<SubTypesAndGoods[]>([])  //每个栏目组成的数组
const cursubType = ref(0)  //当前点击tab
let curUrl = ''// 当前请求地址
// 渲染tab 和 商品
onLoad(async ({ type }) => {
  // 判断typeMap列表里面能不能找到传过来的参数
  const typeItem = typeMap.find(v => v.type === type)
  // console.log(typeItem);
  if (typeItem) {
    curUrl = typeItem.url  //记录请求的地址 后面加载更多需要使用
    const res = await getRecommend(typeItem.url, {})
    console.log('[ res ]-53', res)
    uni.setNavigationBarTitle({ title: res.title })  //修改当前页面标题
    bannerPicture.value = res.bannerPicture  //设置广告图片
    res.subTypes.forEach(item => {
      const obj = {
        id: item.id,
        title: item.title,
        ...item.goodsItems
      }
      subTypes.value.push(obj)
    })
  }
  // console.log(subTypes.value, 'subTypes');
})

// 分页加载  滑动到底部时触发
const onReachBottom = async () => {
  // console.log('加载更多');

  // 记录当前的点击的列表
  const currentsubType = subTypes.value[cursubType.value]
  // console.log('[ currentsubType ]-94', currentsubType)
  if (currentsubType.items.length === currentsubType.counts) return wx.showToast({ title: '我是有底线的哦', icon: 'none' })
  // 请求更多数据
  currentsubType.page++
  const res = await getRecommend(curUrl, { page: currentsubType.page, pageSize: 10, subType: currentsubType.id })
  // console.log(res);
  const item = res.subTypes[cursubType.value].goodsItems.items
  currentsubType.items.push(...item)

}

</script>

<template>
  <view class="viewport">
    <!-- 推荐封面图 -->
    <view class="cover">
      <image :src="bannerPicture"></image>
    </view>
    <view class="tabs">
      <text class="text" v-for="(tab, index) of subTypes" :key="tab.id" :class="{ active: index === cursubType }"
        @click="cursubType = index">{{ tab.title }}</text>
    </view>
    <scroll-view scroll-y enhanced :show-scrollbar="false" class="scroll-view" v-for="(v, i) of subTypes" :key="v.id"
      v-show="i === cursubType" @scrolltolower="onReachBottom">
      <view class="goods">
        <navigator hover-class="none" :url="`/pages/goods/index?id=${item.id}`" class="navigator" v-for="item of v.items"
          :key="item.id">
          <image class="thumb" :src="item.picture">
          </image>
          <view class="name ellipsis">{{ item.name }}</view>
          <view class="price">
            <text class="symbol">¥</text>
            <text class="number">{{ Math.floor(item.price) }}</text>
            <text class="decimal">.00</text>
          </view>
        </navigator>
      </view>
      <view class="loading" v-if="!(subTypes[cursubType]?.items.length === subTypes[cursubType]?.counts)">正在加载...</view>
    </scroll-view>
  </view>
</template>

<style>
page {
  height: 100%;
  background-color: #f4f4f4;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 180rpx 0 0;
  position: relative;
}

.cover {
  width: 750rpx;
  height: 225rpx;
  border-radius: 0 0 40rpx 40rpx;
  overflow: hidden;

  position: absolute;
  left: 0;
  top: 0;
}

.tabs {
  display: flex;
  justify-content: space-evenly;
  height: 100rpx;
  line-height: 90rpx;
  margin: 0 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 5rpx rgba(200, 200, 200, 0.3);
  color: #333;
  background-color: #fff;
  position: relative;
  z-index: 9;
}

.tabs .text {
  margin: 0 20rpx;
  position: relative;
}

.tabs .active::after {
  content: '';
  width: 40rpx;
  height: 4rpx;
  transform: translate(-50%);
  background-color: #27ba9b;
  position: absolute;
  left: 50%;
  bottom: 24rpx;
}

.scroll-view {
  flex: 1;
}

.goods {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx 20rpx;
}

.goods .navigator {
  width: 345rpx;
  padding: 20rpx;
  margin-top: 20rpx;
  border-radius: 10rpx;
  background-color: #fff;
}

.goods .thumb {
  width: 305rpx;
  height: 305rpx;
}

.goods .name {
  height: 88rpx;
  font-size: 26rpx;
}

.goods .price {
  line-height: 1;
  color: #cf4444;
  font-size: 30rpx;
}

.goods .symbol,
.goods .decimal {
  font-size: 70%;
}
</style>
